package satir.command;

import satir.SatirGame;
import satir.framework.command.RegularCommand;
import satir.framework.command.State;
import satir.player.Player;

/**
 * The Class RollDiceCmd.
 * @version 3.0 April 2012
 * @author Creative Family
 */
public class RollDiceCmd implements RegularCommand {

    /** The satir game. */
    private SatirGame satirGame;

    /** The previous moverment. */
    private int previousMoverment;

    /**
     * Instantiates a new roll dice cmd.
     * @param satirGame
     *            the satir game
     */
    public RollDiceCmd(final SatirGame satirGame) {
	this.satirGame = satirGame;
	previousMoverment = 0;
    }

    /**
     * Execute to roll dice.
     */
    public void execute() {
	Player targetPlayer = satirGame.getCurrentPlayer();
	// generate a random dice
	int rollDiceMovement = generateMovement(satirGame.isTestMode());
	// move
	targetPlayer.addMovement(rollDiceMovement);
	// print out result
	System.out.println(targetPlayer.getName() + " roll a number of "
		+ rollDiceMovement + "\n");
	System.out.println("Here is the new position");
	satirGame.getBoard().showBoard();

	// This is the event for the cell appear:
	// We need to discuss and there may be changes
	targetPlayer.getCurrentCell().cellAction(targetPlayer);
    }

    /**
     * Generate movement.
     * @param fix if fix is true, the generated movement will be in sequence of
     * {1, 2, 3 ,4 ,5, 6}
     * @return location
     */
    private int generateMovement(final boolean fix) {
	int rollDiceMovement;
	if (fix) {
	    // Fix the number {1, 2, 3, 4, 5, 6} with the sequence.
	    rollDiceMovement = (++previousMoverment) % 6;
	} else {
	    rollDiceMovement = 1 + (int) (Math.random() * 6);
	}
	return rollDiceMovement;
    }

    /**
     * Gets the instruction.
     * @return instruction
     */
    @Override
    public String getInstruction() {
	return "Roll Dice";
    }

    /**
     * Execute to change state.
     * @param state
     *         the State object
     */
    @Override
    public void execute(final State state) {
	execute();
	state.setState(State.STATE_FINISH);
    }
}
